package com.weihui.jxc.view;

import java.awt.Dimension;

import java.awt.EventQueue;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.List;
import java.util.Vector;

import javax.swing.JInternalFrame;
import javax.swing.JPanel;
import java.awt.BorderLayout;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JComboBox;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JTextField;
import javax.swing.table.AbstractTableModel;

import com.weihui.jxc.common.CommonFactory;
import com.weihui.jxc.common.ValidationManager;
import com.weihui.jxc.model.Customer;
import com.weihui.jxc.model.GoodsInfo;
import com.weihui.jxc.model.InAndOut;
import com.weihui.jxc.model.InAnOutInfo;
import com.weihui.jxc.model.Provider;
import com.weihui.jxc.service.CustomerService;
import com.weihui.jxc.service.GoodsService;
import com.weihui.jxc.service.OutportService;
import com.weihui.jxc.service.ProviderService;

import javax.swing.JButton;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import java.awt.FlowLayout;
import javax.swing.JCheckBox;
import java.awt.SystemColor;
import javax.swing.ImageIcon;
/**
 * 出库信息查询界面

 */
public class OutportInforSearchFrame extends JInternalFrame implements ActionListener{
	private JTextField searchValue;
	private JComboBox comboBox;//查询选项
    private JTable table;//表格一定要放在JScrollPane，这样数据多的时候，就可以滑动表格
    private MyTableModel tableModel;//自定义的表格模板，为了适配数据
    private OutportService outportService;
    private JTextField startTimeTf;
    private JTextField endTimeTf;
    private JCheckBox chckbxNewCheckBox;
	/**
	 * 初始化视窗
	 */
	public OutportInforSearchFrame() {
		//支持放大缩小、可关闭
		super("出库查询", true, true, true, true);
		//获取屏幕尺寸
		Dimension screenSize =Toolkit.getDefaultToolkit().getScreenSize();
		setBounds(0,0,screenSize.width*2/3,screenSize.height*2/3);
		
		JPanel panel = new JPanel();
		panel.setBackground(SystemColor.inactiveCaptionBorder);
		getContentPane().add(panel, BorderLayout.NORTH);
		
		JLabel lblNewLabel = new JLabel("请选择查询条件：");
	//	lblNewLabel.setIcon(new ImageIcon(OutportInforSearchFrame.class.getResource("/images/\u67E5\u8BE2\u6761\u4EF6.png")));
		
		comboBox = new JComboBox();
		comboBox.setModel(new DefaultComboBoxModel(new String[] {"出库单号", "供应商名称", "商品名称", "经手人"}));
		
		JButton searchOne = new JButton("查询");
		//给“查询”按钮绑定监听事件
		searchOne.addActionListener(this);
		
		JButton searchList = new JButton("显示全部信息");
		//给“显示全部信息”按钮绑定监听事件
		searchList.addActionListener(this);
		panel.setLayout(new FlowLayout(FlowLayout.CENTER, 5, 5));
		panel.add(lblNewLabel);
		panel.add(comboBox);
		
		searchValue = new JTextField();
		searchValue.setColumns(18);
		panel.add(searchValue);
		
		chckbxNewCheckBox = new JCheckBox("指定查询日期：");
		panel.add(chckbxNewCheckBox);
		
		JLabel lblNewLabel_1 = new JLabel("从");
		panel.add(lblNewLabel_1);
		
		startTimeTf = new JTextField();
		panel.add(startTimeTf);
		startTimeTf.setColumns(18);
		
		JLabel lblNewLabel_2 = new JLabel("到");
		panel.add(lblNewLabel_2);
		
		endTimeTf = new JTextField();
		endTimeTf.setColumns(18);
		panel.add(endTimeTf);
		panel.add(searchOne);
		panel.add(searchList);
		//创建表格的列（标题栏文字）
		String[] columnNames = {"出库单号","商品名称","商品数量","合计金额","供应商名称","验收结论","出库时间","经手人",
				"支付方式","操作人员"};
		//实例化模板
		tableModel = new MyTableModel<InAnOutInfo>(columnNames, InAnOutInfo.class);
		//创建表格面板
		table = new JTable(tableModel);
		//设置表格的大小
		table.setPreferredScrollableViewportSize(new Dimension(screenSize.width*2/3-60, screenSize.height/3));
		//视图高度填充
		table.setFillsViewportHeight(true);
		table.setAutoCreateRowSorter(true);
		//将表格放到滑动面板中
		JScrollPane scrollPane = new JScrollPane(table);
		getContentPane().add(scrollPane, BorderLayout.CENTER);
		//实例化service
		outportService = CommonFactory.getOutportService();
		//更新数据
		//updateAllData(null);
	}
	
	/**
	 * 点击事件
	 */
    @Override
    public void actionPerformed(ActionEvent e) {
    	String target = e.getActionCommand();
    	switch (target) {
		case "查询":
			//封装查询的数据
			InAnOutInfo c = new InAnOutInfo();
			if(chckbxNewCheckBox.isSelected()) {//根据指定日期查询
				SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
				String startTime = startTimeTf.getText().trim();
				String endTime = endTimeTf.getText().trim();
				if(startTime==null || startTime.length()==0) {
					JOptionPane.showMessageDialog(null, "请输入开始时间","警告",JOptionPane.WARNING_MESSAGE);
					return;
				}
				if(endTime==null || endTime.length()==0) {
					JOptionPane.showMessageDialog(null, "请输入结束时间","警告",JOptionPane.WARNING_MESSAGE);
					return;
				}
				if(!ValidationManager.validateDate(startTime)) {
					JOptionPane.showMessageDialog(null, "时间格式不正确，正确格式：yyyy-MM-dd","警告",JOptionPane.WARNING_MESSAGE);
					return;
				}
				if(!ValidationManager.validateDate(endTime)) {
					JOptionPane.showMessageDialog(null, "时间格式不正确，正确格式：yyyy-MM-dd","警告",JOptionPane.WARNING_MESSAGE);
					return;
				}
				try {
					c.setStartTime(sdf.parse(startTime));
					c.setEndTime(sdf.parse(endTime));
				} catch (ParseException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				}
			}else {//根据条件查询
				//搜索框非空验证
				String searchVal = searchValue.getText().trim();
				if(searchVal==null || searchVal.length()==0) {
					JOptionPane.showMessageDialog(null, "请输入搜索的值","警告",JOptionPane.WARNING_MESSAGE);
					return;
				}
				String type = comboBox.getSelectedItem().toString();//获取查询类型
				switch (type) {
				case "出库单号":
					c.setId(searchVal);
					break;
	            case "供应商名称":
					c.setProviderName(searchVal);
					break;
	            case "商品名称":
					c.setGoodsName(searchVal);
					break;
	            case "经手人":
					c.setSponsor(searchVal);
					break;
				default:
					break;
				}
			}
			//更新数据
			updateAllData(c);
			break;
		case "显示全部信息":
			//更新数据
			updateAllData(null);
			break;
		default:
			break;
		}
    	
    }
    /**
     * 更新表格数据
     * @param c
     */
	private void updateAllData(InAnOutInfo i) {
		//查询数据库
		List<InAnOutInfo> outportInfos = outportService.queryAll(i);
		if(outportInfos==null || outportInfos.size()==0) {
			JOptionPane.showMessageDialog(null, "没有满足你条件的出库单记录","警告",JOptionPane.WARNING_MESSAGE);
			//清空输入框中的值
			searchValue.setText("");
			return;
		}
		Vector<InAnOutInfo> list = new Vector<InAnOutInfo>();
			for (InAnOutInfo customer : outportInfos) {
				list.add(customer);
			}
			//更新数据
			tableModel.updateData(list);
			//清空输入框中的值
			searchValue.setText("");
		}
	}
